home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-19 / rcs55.zip / MAKETIME.C < prev    next >
C/C++ Source or Header  |  1991-09-15  |  9KB  |  312 lines

  1. #
  2. /*
  3.  * MAKETIME        derive 32-bit time value from TM structure.
  4.  *
  5.  * Usage:
  6.  *    int zone;    Minutes west of GMT, or
  7.  *            48*60 for localtime
  8.  *    time_t t;
  9.  *    struct tm *tp;    Pointer to TM structure from <time.h>
  10.  *    t = maketime(tp,zone);
  11.  *
  12.  * Returns:
  13.  *    -1 if failure; parameter out of range or nonsensical.
  14.  *    else time-value.
  15.  * Notes:
  16.  *    This code is quasi-public; it may be used freely in like software.
  17.  *    It is not to be sold, nor used in licensed software without
  18.  *    permission of the author.
  19.  *    For everyone's benefit, please report bugs and improvements!
  20.  *     Copyright 1981 by Ken Harrenstien, SRI International.
  21.  *    (ARPANET: KLH @ SRI)
  22.  */
  23. /* $Log: maketime.c%v $
  24.  * Revision 1.2  1991/08/23  13:31:02  SGP
  25.  * Ported to MSDOS using Borland C++
  26.  *
  27.  * Revision 5.2  1990/11/01  05:03:30  eggert
  28.  * Remove lint.
  29.  *
  30.  * Revision 5.1  1990/10/04  06:30:13  eggert
  31.  * Calculate the GMT offset of 'xxx LT' as of xxx, not as of now.
  32.  * Don't assume time_t is 32 bits.  Fix bugs near epoch and near end of time.
  33.  *
  34.  * Revision 5.0  1990/08/22  08:12:38  eggert
  35.  * Switch to GMT and fix the bugs exposed thereby.
  36.  * Permit dates past 1999/12/31.  Ansify and Posixate.
  37.  *
  38.  * Revision 1.8  88/11/08  13:54:53  narten
  39.  * allow negative timezones (-24h <= x <= 24h)
  40.  * 
  41.  * Revision 1.7  88/08/28  14:47:52  eggert
  42.  * Allow cc -R.  Remove unportable "#endif XXX"s.
  43.  * 
  44.  * Revision 1.6  87/12/18  17:05:58  narten
  45.  * include rcsparam.h
  46.  * 
  47.  * Revision 1.5  87/12/18  11:35:51  narten
  48.  * maketime.c: fixed USG code - you have tgo call "tzset" in order to have
  49.  * "timezone" set. ("localtime" calls it, but it's probably better not to 
  50.  * count on "localtime" having been called.)
  51.  * 
  52.  * Revision 1.4  87/10/18  10:26:57  narten
  53.  * Updating version numbers. Changes relative to 1.0 are actually 
  54.  * relative to 1.2
  55.  * 
  56.  * Revision 1.3  87/09/24  13:58:45  narten
  57.  * Sources now pass through lint (if you ignore printf/sprintf/fprintf 
  58.  * warnings)
  59.  * 
  60.  * Revision 1.2  87/03/27  14:21:48  jenkins
  61.  * Port to suns
  62.  * 
  63.  * Revision 1.2  83/12/05  10:12:56  wft
  64.  * added cond. compilation for USG Unix; long timezone;
  65.  * 
  66.  * Revision 1.1  82/05/06  11:38:00  wft
  67.  * Initial revision
  68.  * 
  69.  */
  70.  
  71.  
  72. #include "rcsbase.h"
  73.  
  74. libId(maketId, "$Id: maketime.c%v 1.2 1991/08/23 13:31:02 SGP Exp $")
  75.  
  76. static const struct tm *time2tm P((time_t));
  77.  
  78. #define given(v) (0 <= (v)) /* Negative values are unspecified. */
  79.  
  80. static const int daytb[] = {
  81.     /* # days in year thus far, indexed by month (0-12!!) */
  82.     0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365
  83. };
  84.  
  85.     static time_t
  86. maketime(const struct tm *atm,int zone)
  87. {
  88.     register const struct tm *tp;
  89.     register int i;
  90.     int year, yday, mon, day, hour, min, sec, leap, localzone;
  91.     int attempts;
  92.     time_t t, tres;
  93.  
  94.     attempts = 2;
  95.     localzone = zone==48*60;
  96.     tres = -1;
  97.     year = mon = day = 0;  /* Keep lint happy.  */
  98.  
  99.     do {
  100.  
  101.     if (localzone || !given(atm->tm_year)) {
  102.         if (tres == -1)
  103.             if ((tres = time((time_t*)0))  ==  -1)
  104.                 return -1;
  105.         tp = time2tm(tres);
  106.         /* Get breakdowns of default time, adjusting to zone. */
  107.         year = tp->tm_year;        /* Use to set up defaults */
  108.         yday = tp->tm_yday;
  109.         mon = tp->tm_mon;
  110.         day = tp->tm_mday;
  111.         hour = tp->tm_hour;
  112.         min = tp->tm_min;
  113.         if (localzone) {
  114.             tp = localtime(&tres);
  115.             zone =
  116.             min - tp->tm_min + 60*(
  117.                 hour - tp->tm_hour + 24*(
  118.                     /* If years differ, it's by one day. */
  119.                         year - tp->tm_year
  120.                     ?    year - tp->tm_year
  121.                     :    yday - tp->tm_yday));
  122.         }
  123.         /* Adjust the default day, month and year according to zone.  */
  124.         if ((min -= zone) < 0) {
  125.             if (hour-(59-min)/60 < 0  &&  --day <= 0) {
  126.             if (--mon < 0) {
  127.                 --year;
  128.                 mon = 11;
  129.             }
  130.             day  =  daytb[mon+1] - daytb[mon] + (mon==1&&!(year&3));
  131.             }
  132.         } else
  133.             if (
  134.               24 <= hour+min/60  &&
  135.               daytb[mon+1] - daytb[mon] + (mon==1&&!(year&3))  <  ++day
  136.             ) {
  137.                 if (11 < ++mon) {
  138.                     ++year;
  139.                     mon = 0;
  140.                 }
  141.                 day = 1;
  142.             }
  143.     }
  144.     if (zone < -24*60  ||  24*60 < zone)
  145.         return -1;
  146.  
  147.  
  148. #ifdef DEBUG
  149. printf("first YMD: %d %d %d\n",year,mon,day);
  150. #endif
  151.     tp = atm;
  152.  
  153.     /* First must find date, using specified year, month, day.
  154.      * If one of these is unspecified, it defaults either to the
  155.      * current date (if no more global spec was given) or to the
  156.      * zero-value for that spec (i.e. a more global spec was seen).
  157.      * Reject times that do not fit in time_t,
  158.      * without assuming that time_t is 32 bits or is signed.
  159.      */
  160.     if (given(tp->tm_year))
  161.       {
  162.         year = tp->tm_year;
  163.         mon = 0;        /* Since year was given, default */
  164.         day = 1;        /* for remaining specs is zero */
  165.       }
  166.     if (year < 69)            /* 1969/12/31 OK in some timezones.  */
  167.         return -1;        /* ERR: year out of range */
  168.     leap   =   !(year&3)  &&  (year%100 || !((year+300)%400));
  169.     year -= 70;            /* UNIX time starts at 1970 */
  170.  
  171.     /*
  172.      * Find day of year.
  173.      */
  174.     {
  175.         if (given(tp->tm_mon))
  176.           {    mon = tp->tm_mon;    /* Month was specified */
  177.             day = 1;        /* so set remaining default */
  178.           }
  179.         if (11 < (unsigned)mon)
  180.             return -1;        /* ERR: bad month */
  181.         if (given(tp->tm_mday)) day = tp->tm_mday;
  182.         if(day < 1
  183.          || (((daytb[mon+1]-daytb[mon]) < day)
  184.             && (day!=29 || mon!=1 || !leap) ))
  185.                 return -1;    /* ERR: bad day */
  186.         yday = daytb[mon]    /* Add # of days in months so far */
  187.           + ((leap        /* Leap year, and past Feb?  If */
  188.               && mon>1)? 1:0)    /* so, add leap day for this year */
  189.           + day-1;        /* And finally add # days this mon */
  190.  
  191.     }
  192.     if (leap+365 <= (unsigned)yday)
  193.         return -1;        /* ERR: bad YDAY */
  194.  
  195.     if (year < 0) {
  196.         if (yday != 364)
  197.         return -1;        /* ERR: too early */
  198.         t = -1;
  199.     } else {
  200.         tres = year*365;        /* Get # days of years so far */
  201.         if (tres/365 != year)
  202.             return -1;        /* ERR: overflow */
  203.         t = tres
  204.         + ((year+1)>>2)        /* plus # of leap days since 1970 */
  205.         + yday;            /* and finally add # days this year */
  206.         if (t+4 < tres)
  207.             return -1;        /* ERR: overflow */
  208.     }
  209.     tres = t;
  210.  
  211.     if (given(i = tp->tm_wday)) /* Check WDAY if present */
  212.         if (i != (tres+4)%7)    /* 1970/01/01 was Thu = 4 */
  213.             return -1;    /* ERR: bad WDAY */
  214.  
  215. #ifdef DEBUG
  216. printf("YMD: %d %d %d, T=%ld\n",year,mon,day,tres);
  217. #endif
  218.     /*
  219.      * Now determine time.  If not given, default to zeros
  220.      * (since time is always the least global spec)
  221.      */
  222.     tres *= 86400L;            /* Get # seconds (24*60*60) */
  223.     if (tres/86400L != t)
  224.         return -1;        /* ERR: overflow */
  225.     hour = min = sec = 0;
  226.     if (given(tp->tm_hour)) hour = tp->tm_hour;
  227.     if (given(tp->tm_min )) min  = tp->tm_min;
  228.     if (given(tp->tm_sec )) sec  = tp->tm_sec;
  229.     if (60 <= (unsigned)min  ||  60 < (unsigned)sec)
  230.         return -1;        /* ERR: MS out of range */
  231.     if (24 <= (unsigned)hour)
  232.         if(hour != 24 || (min+sec) !=0)    /* Allow 24:00 */
  233.             return -1;    /* ERR: H out of range */
  234.  
  235.     t = tres;
  236.     tres += sec + 60L*(zone + min + 60*hour);
  237.  
  238. #ifdef DEBUG
  239. printf("HMS: %d %d %d T=%ld\n",hour,min,sec,tres);
  240. #endif
  241.  
  242.     if (!localzone)            /* check for overflow */
  243.         return (year<0 ? (tres<0||86400L<=tres) : tres<t)  ?  -1  :  tres;
  244.  
  245.     /* Check results; LT may have had a different GMT offset back then.  */
  246.     tp = localtime(&tres);
  247.     if (given(atm->tm_sec)  &&  atm->tm_sec != tp->tm_sec)
  248.         return -1; /* If seconds don't match, we're in trouble.  */
  249.     if (!(
  250.         given(atm->tm_min)  &&  atm->tm_min != tp->tm_min  ||
  251.         given(atm->tm_hour)  &&  atm->tm_hour != tp->tm_hour  ||
  252.         given(atm->tm_mday)  &&  atm->tm_mday != tp->tm_mday  ||
  253.         given(atm->tm_mon)  &&  atm->tm_mon != tp->tm_mon  ||
  254.         given(atm->tm_year)  &&  atm->tm_year != tp->tm_year
  255.     ))
  256.         return tres; /* Everything matches.  */
  257.  
  258.     } while (--attempts);
  259.  
  260.     return -1;
  261. }
  262.  
  263. /*
  264. * Convert Unix time to struct tm format.
  265. * Use Coordinated Universal Time (UTC) if available and if version 5 or newer;
  266. * use local time otherwise.
  267. */
  268.     static const struct tm *
  269. time2tm(time_t unixtime)
  270. {
  271.     const struct tm *tm;
  272.     return
  273.             VERSION(5)<=RCSversion && (tm = gmtime(&unixtime))
  274.         ?    tm
  275.         :    localtime(&unixtime);
  276. }
  277.  
  278. /*
  279. * Convert Unix time to RCS format.
  280. * For compatibility with older versions of RCS,
  281. * dates before AD 2000 are stored without the leading "19".
  282. */
  283.     void
  284. time2date(time_t unixtime,char date[datesize])
  285. {
  286.     register const struct tm *tm = time2tm(unixtime);
  287.     VOID sprintf(date, DATEFORM,
  288.         tm->tm_year  +  (tm->tm_year<100 ? 0 : 1900),
  289.         tm->tm_mon+1, tm->tm_mday,
  290.         tm->tm_hour, tm->tm_min, tm->tm_sec
  291.     );
  292. }
  293.  
  294.  
  295.  
  296.     void
  297. str2date(const char *source, char target[datesize])
  298. /* Parse a free-format date in SOURCE, convert it
  299.  * into RCS internal format, and store the result into TARGET.
  300.  */
  301. {
  302.     int zone;
  303.     time_t unixtime;
  304.     struct tm parseddate;
  305.  
  306.     if (!partime(source, &parseddate, &zone))
  307.         faterror("can't parse date/time: %s", source);
  308.     if ((unixtime = maketime(&parseddate, zone))  ==  -1)
  309.         faterror("bad date/time: %s", source);
  310.     time2date(unixtime, target);
  311. }
  312.